package workCount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class wordCount {
    Configuration conf;



    wordCount(String URL) throws URISyntaxException, IOException, InterruptedException {
        conf = new Configuration();
        conf.set("fs.defaultFS", URL);
        conf.set("dfs.replication", "1");

    }

    public void wordCount_main(String filename, String jobName, String destDir) throws IOException, InterruptedException, ClassNotFoundException {
        //定义一个任务。wordcount任务
        Job job = Job.getInstance(conf, jobName);
        job.setJarByClass(wordCount.class);

        //定义任务的来源
        FileInputFormat.addInputPath(job, new Path(filename));
        //定义处理完成任务的去向
        FileOutputFormat.setOutputPath(job, new Path(destDir));

        //任务
        //设置Mapper的传递
        job.setMapperClass(mapper.class);
        //输出的是 hadoop 1
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //设置Reduce的操作
        job.setReducerClass(reducer.class);
        //设置hadoop 3 的输出
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //执行任务操作
        job.waitForCompletion(true);
    }

    //定义mapper操作
    public static class mapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        //重写map操作
        //Context:上下文，传递进来的内容向下一阶段传递
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
                throws IOException, InterruptedException {
            String str = value.toString();
            String[] strArr = str.split(" ");
            Text text;
            IntWritable intWritable;
            for (String s : strArr) {
                text = new Text(s);
                intWritable = new IntWritable(1);
                context.write(text, intWritable);
            }
        }
    }

    //定义Reduce操作
    //处理来自Shuffle处理完的Mapper操作的数据
    //需要实现一个功能，hadoop1 1 1 1 --> 求和 --> hadoop4
    //Reducer<k1,v1,k2,v2> --> k1,v1 Reducer的输入，同时也是Mapper的输出 k2 v2
    public static class reducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable tmp : values) {
                sum += tmp.get();
            }
            context.write(key, new IntWritable(sum));
        }
    }
}

